extends CharacterBody2D
signal hit

@export var speed = 300.0
@export var jump_velocity = -400.0
@export var screen_size = 0;
@export var max_health = 100.0;
var health = 0;
var player_vec = Vector2.ZERO;
var death_gui = preload("res://Scene/DeathGUI.tscn");

func _ready() -> void:
	health = max_health
	screen_size = get_viewport_rect().size;

func _process(delta: float) -> void:
	if Input.is_action_pressed("skill_use"):
		position = get_global_mouse_position()

func _physics_process(delta: float) -> void:
	if not is_on_floor():
		velocity += get_gravity() * delta

	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = jump_velocity

	var direction := Input.get_axis("move_left", "move_right")
	if direction: #move
		velocity.x = direction * speed
		$AnimatedSprite2D.play("move")
	else:
		velocity.x = move_toward(velocity.x, 0, speed)
		$AnimatedSprite2D.stop()

	move_and_slide()

func start(pos):
	position = pos;
	show();
	$CollisionShape2D.disabled = false;

func get_health() -> float:
	return health

func get_max_health() -> float:
	return max_health

func damage(value: float):
	health -= value
	$AudioStreamPlayer2D.play()
	%Hud.update_health_bar()
	if (health <= 0):
		queue_free()
		var instance = death_gui.instantiate()
		get_tree().root.add_child(instance)

func _on_hurt_box_hurt() -> void:
	damage(10.0)
	print(health)
